<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="formdata" label-width="100px">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userinfo.departmentName" @focus="show = true" />
            <div v-if="show" class="box">
              <i class="el-icon-circle-close" @click="show = false" />
              <el-tree
                :data="depts"
                :props="defaultProps"
                @node-click="handleNodeClick"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userinfo.correctionTime"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <el-image :src="require('@/assets/common/head.jpg')" /> -->
            <UploadImage
              :staffphoto="userinfo.staffPhoto"
              @uploaded="uploaded"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUpdate">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getDepartments } from '@/api/table'
import { getDepart } from '@/utils'
import { saveUserDetailById } from '@/api/employees'
import { mapActions } from 'vuex'
export default {
  props: {
    userinfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      show: false,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getDepartmentsList()
  },
  methods: {
    ...mapActions('user', ['getUserInfo']),
    uploaded (img) {
      this.userinfo.staffPhoto = img
      console.log(img)
    },
    async saveUpdate () {
      try {
        await this.$refs.formdata.validate()
        await saveUserDetailById(this.userinfo)
        this.getUserInfo()
        this.$message.success('修改成功')
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    },
    async getDepartmentsList () {
      try {
        const res = await getDepartments()
        console.log(res.depts)
        this.depts = getDepart(res.depts)
      } catch (error) {
        console.log(error)
      }
    },
    handleNodeClick (node) {
      console.log(node)
      if (!node.children) {
        this.userinfo.departmentName = node.name
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  position: relative;
  .el-icon-circle-close {
    position: absolute;
    top: 6px;
    right: 6px;
    z-index: 999;
    cursor: pointer;
  }
}
</style>
